<?php
/**
 * 订单中心
 */
class wap_ctl_order extends wap_controller
{
    var $delivery_link    = array();
    
    function __construct($app)
    {
        parent::__construct($app);
        
        //地图
        $this->delivery_link['mapLink']    = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'showMapByBaidu'), true);
        
        //确认拒单
        $this->delivery_link['doRefuse']   = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'doRefuse'), true);
        
        //立即接单
        $this->delivery_link['doConfirm']   = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'doConfirm'), true);
        
        //发货
        $this->delivery_link['doConsign']    = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'doConsign'), true);
        
        //签收
        $this->delivery_link['signPage']    = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'signPage'), true);
        $this->delivery_link['doSign']         = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'doSign'), true);
        $this->delivery_link['sendMsg']        = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'sendMsg'), true);
        $this->delivery_link['showOrderInfo']  = app::get('wap')->router()->gen_url(array('ctl'=>'order','act'=>'showOrderInfo'), true);
        
        $this->pagedata['delivery_link']   = $this->delivery_link;
    }
    
    /**
     * 条件
     * 
     * status 0:未处理  1:打回  2:暂停 3:已发货
     */
    function _views_confirm($curr_view)
    {
        $base_filter = array();
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        
        $page              = intval($_POST['page']) ? intval($_POST['page']) : 0;
        $limit             = 1;//每页显示数量
        $offset            = $limit * $page;
        
        $is_super    = kernel::single('desktop_user')->is_super();
        if(!$is_super)
        {
            $branchObj     = kernel::single('o2o_store_branch');
            $branch_ids    = $branchObj->getO2OBranchByUser(true);
            if(empty($branch_ids))
            {
                $this->pagedata['link_url']     = $this->delivery_link['order_index'];
                $this->pagedata['error_msg']    = '操作员没有管辖的仓库';
                echo $this->fetch('auth_error.html');
                exit;
            }
            $base_filter['branch_id']    = $branch_ids[0];
        }
        
        //超时订单
        if($curr_view == 'overtime')
        {
            $dly_overtime    = 0;
            
            //履约超时时间设置(分钟)
            $minute    = app::get('o2o')->getConf('o2o.delivery.dly_overtime');
            $minute    = intval($minute);
            
            if($minute)
            {
                $second          = $minute * 60;
                $dly_overtime    = time() - $second;//现在时间 减去 履约时间
            }
        }
        
        //menu
        $sub_menu = array(
                'all' => array('label'=>app::get('base')->_('订单查看'), 'filter'=>array('status'=>array(0, 3), 'confirm'=>array(1, 3)), 'href'=>$this->delivery_link['order_index']),
                'confirm' => array('label'=>app::get('base')->_('订单确认') ,'filter'=>array('status'=>0, 'confirm'=>3), 'href'=>$this->delivery_link['order_confirm']),
                'consign' => array('label'=>app::get('base')->_('订单发货') ,'filter'=>array('status'=>0, 'confirm'=>1), 'href'=>$this->delivery_link['order_consign']),
                'sign' => array('label'=>app::get('base')->_('签收核销') ,'filter'=>array('status'=>3, 'confirm'=>1, 'process_status'=>7, 'is_received'=>1), 'href'=>$this->delivery_link['order_sign']),
                'overtime' => array('label'=>app::get('base')->_('超时订单') ,'filter'=>array('status'=>0, 'confirm'=>array(1, 3), 'create_time|lthan'=>$dly_overtime), 'href'=>$this->delivery_link['overtimeOrders']),
        );
        
        foreach($sub_menu as $k=>$v)
        {
            //Ajax加载下一页数据,只处理本页
            if($_POST['flag'] == 'ajax' && $curr_view != $k)
            {
                continue;
            }
            
            if (!IS_NULL($v['filter']))
            {
                $v['filter']    = array_merge($v['filter'], $base_filter);
            }
            
            //搜索条件
            if($_POST['sel_type'] && $_POST['sel_keywords'])
            {
                switch ($_POST['sel_type'])
                {
                    case 'delivery_bn':
                        $v['filter']['delivery_bn']    = htmlspecialchars(trim($_POST['sel_keywords']));
                    break;
                    case 'order_bn':
                        $v['filter']['order_bn']    = htmlspecialchars(trim($_POST['sel_keywords']));
                    break;
                    case 'ship_mobile':
                        $v['filter']['ship_mobile']    = htmlspecialchars(trim($_POST['sel_keywords']));
                    break;
                    case 'ship_name':
                        $v['filter']['ship_name']    = htmlspecialchars(trim($_POST['sel_keywords']));
                    break;
                }
            }
            
            $count    = $wapDeliveryObj->count($v['filter']);
            
            $sub_menu[$k]['filter']    = $v['filter'] ? $v['filter'] : null;
            $sub_menu[$k]['count']     = $count;
            $sub_menu[$k]['pageSize']  = ceil($count / $limit);
            
            $sub_menu[$k]['offset']    = $offset;
            $sub_menu[$k]['limit']     = $limit;
            $sub_menu[$k]['orderby']   = 'delivery_id desc';#排序
            
            if($k == $curr_view){
                $sub_menu[$k]['curr_view'] = true;
            }else{
                $sub_menu[$k]['curr_view'] = false;
            }
        }
        
        return $sub_menu;
    }

    /**
     * 订单查看
     */
    function index()
    {
        $this->delivery_type    = 'all';
        
        $sub_menu    = $this->_views_confirm($this->delivery_type);
        $filter      = $sub_menu[$this->delivery_type]['filter'];
        $title       = $sub_menu[$this->delivery_type]['label'];
        
        $offset            = $sub_menu[$this->delivery_type]['offset'];
        $limit             = $sub_menu[$this->delivery_type]['limit'];
        
        #仓库对应_发货单列表
        $wapDeliveryLib    = kernel::single('wap_delivery');
        
        $dataList          = $wapDeliveryLib->getList($filter, $offset, $limit, $sub_menu[$this->delivery_type]['orderby']);
        
        $this->pagedata['title']       = $title;
        $this->pagedata['dataList']    = $dataList;
        $this->pagedata['pageSize']    = $sub_menu[$this->delivery_type]['pageSize'];
        
        $this->pagedata['link_url']    = $sub_menu[$this->delivery_type]['href'];
        
        //baidu map button show or not
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show=="true"){
            $this->pagedata["baidu_map_show"] = true;
        }
        
        if($offset > 0)
        {
            //Ajax加载更多
            $this->display('order/order_list_more.html');
        }
        else
        {
            //门店核单拒绝原因
            $reasonObj    = app::get('o2o')->model('refuse_reason');
            $refuse_reasons  = $reasonObj->getList('*', array('disabled'=>'false'), 0, 100);
            $this->pagedata['refuse_reasons']    = $refuse_reasons;
            
            $this->display('order/order_list.html');
        }
    }
    
    /**
     * 订单确认
     */
    function confirm()
    {
        $this->delivery_type    = 'confirm';
        
        $sub_menu    = $this->_views_confirm($this->delivery_type);
        $filter      = $sub_menu[$this->delivery_type]['filter'];
        $title       = $sub_menu[$this->delivery_type]['label'];
        
        $offset            = $sub_menu[$this->delivery_type]['offset'];
        $limit             = $sub_menu[$this->delivery_type]['limit'];
        
        #仓库对应_发货单列表
        $wapDeliveryLib    = kernel::single('wap_delivery');
        
        $dataList          = $wapDeliveryLib->getList($filter, $offset, $limit, $sub_menu[$this->delivery_type]['orderby']);
        
        $this->pagedata['title']       = $title;
        $this->pagedata['dataList']    = $dataList;
        $this->pagedata['pageSize']    = $sub_menu[$this->delivery_type]['pageSize'];
        
        $this->pagedata['link_url']    = $sub_menu[$this->delivery_type]['href'];
        
        //baidu map button show or not
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show=="true"){
            $this->pagedata["baidu_map_show"] = true;
        }
        
        if($offset > 0)
        {
            //Ajax加载更多
            $this->display('order/order_list_more.html');
        }
        else
        {
            //门店核单拒绝原因
            $reasonObj    = app::get('o2o')->model('refuse_reason');
            $refuse_reasons  = $reasonObj->getList('*', array('disabled'=>'false'), 0, 100);
            $this->pagedata['refuse_reasons']    = $refuse_reasons;
            
            $this->display('order/order_list.html');
        }
    }
    
    /**
     * 订单发货
     */
    function consign()
    {
        $this->delivery_type    = 'consign';
        
        $sub_menu    = $this->_views_confirm($this->delivery_type);
        $filter      = $sub_menu[$this->delivery_type]['filter'];
        $title       = $sub_menu[$this->delivery_type]['label'];
        
        $offset            = $sub_menu[$this->delivery_type]['offset'];
        $limit             = $sub_menu[$this->delivery_type]['limit'];
        
        #仓库对应_发货单列表
        $wapDeliveryLib    = kernel::single('wap_delivery');
        
        $dataList          = $wapDeliveryLib->getList($filter, $offset, $limit, $sub_menu[$this->delivery_type]['orderby']);
        
        $this->pagedata['title']       = $title;
        $this->pagedata['dataList']    = $dataList;
        $this->pagedata['pageSize']    = $sub_menu[$this->delivery_type]['pageSize'];
        
        $this->pagedata['link_url']    = $sub_menu[$this->delivery_type]['href'];
        
        //baidu map button show or not
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show=="true"){
            $this->pagedata["baidu_map_show"] = true;
        }
        
        if($offset > 0)
        {
            //Ajax加载更多
            $this->display('order/order_list_more.html');
        }
        else
        {
            $this->display('order/order_list.html');
        }
    }
    
    /**
     * 确认拒单
     *
     * @param intval  $delivery_id
     * @return json
     */
    function doRefuse()
    {
        $delivery_id    = intval($_POST['delivery_id']);
        $redirect_url   = ($_POST['backUrl'] ? $_POST['backUrl'] : $this->delivery_link['order_confirm']);
        if(empty($delivery_id))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'无效操作'));
            exit;
        }
        
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump(array('delivery_id'=>$delivery_id), '*');
        if(empty($deliveryInfo))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'没有相关发货单'));
            exit;
        }
        elseif($deliveryInfo['status'] > 0 || $deliveryInfo['confirm'] != 3)
        {
            echo json_encode(array('res'=>'error', 'msg'=>'该发货单无法继续操作'));
            exit;
        }

        $dlyProcessLib = kernel::single('wap_delivery_process');
        
        //组织参数
        $params = array_merge(array('delivery_id'=>$delivery_id), $deliveryInfo);
        
        $refuse_reason_id    = intval($_POST['refuse_reason_id']);
        if(empty($refuse_reason_id))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'请选择拒单理由'));
            exit;
        }
        
        //拒绝原因
        $params['reason_id']   = $refuse_reason_id;
        
        if($dlyProcessLib->refuse($params)){
            
            //task任务更新统计数据
            $wapDeliveryLib    = kernel::single('wap_delivery');
            $wapDeliveryLib->taskmgr_statistic('refuse');
            
            echo json_encode(array('res'=>'succ', 'status'=>'已拒绝', 'msg'=>'已拒绝成功'));
            exit;
        }else{
            echo json_encode(array('res'=>'error', 'msg'=>'门店拒绝失败'));
            exit;
        }
    }
    
    /**
     * 立即接单
     * 
     * @return json
     */
    function doConfirm()
    {
        $delivery_id    = intval($_POST['delivery_id']);
        $redirect_url   = ($_POST['backUrl'] ? $_POST['backUrl'] : $this->delivery_link['order_confirm']);
        if(empty($delivery_id))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'无效操作'));
            exit;
        }
        
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump(array('delivery_id'=>$delivery_id), '*');
        if(empty($deliveryInfo))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'没有相关发货单'));
            exit;
        }
        elseif($deliveryInfo['status'] > 0 || $deliveryInfo['confirm'] != 3)
        {
            echo json_encode(array('res'=>'error', 'msg'=>'该发货单无法继续操作'));
            exit;
        }

        $dlyProcessLib = kernel::single('wap_delivery_process');
        
        //组织参数
        $params = array_merge(array('delivery_id'=>$delivery_id), $deliveryInfo);
        
        if($dlyProcessLib->accept($params)){
            
            //task任务更新统计数据
            $wapDeliveryLib    = kernel::single('wap_delivery');
            $wapDeliveryLib->taskmgr_statistic('confirm');
            
            echo json_encode(array('res'=>'succ', 'status'=>'已确认', 'msg'=>'订单已接收', 'delivery_bn'=>$deliveryInfo['delivery_bn']));
            exit;
        }else{
            echo json_encode(array('res'=>'error', 'msg'=>'门店确认失败'));
            exit;
        }
    }

    /** 
     * 立即发货
     * 
     * @return json
     */
    function doConsign()
    {
        $delivery_id    = intval($_POST['delivery_id']);
        $redirect_url   = ($_POST['backUrl'] ? $_POST['backUrl'] : $this->delivery_link['order_consign']);
        if(empty($delivery_id))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'无效操作'));
            exit;
        }
        $filter    = array('delivery_id'=>$delivery_id);
        
        #管理员对应仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if(!$is_super){
            $branchObj     = kernel::single('o2o_store_branch');
            $branch_ids    = $branchObj->getO2OBranchByUser(true);
            if(empty($branch_ids))
            {
                $error_msg    = '操作员没有管辖的仓库';
                
                echo json_encode(array('res'=>'error', 'msg'=>$error_msg));
                exit;
            }
            $filter['branch_id'] = $branch_ids[0];
        }
        
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump(array('delivery_id'=>$filter), '*');
        $deliveryInfo['status']    = intval($deliveryInfo['status']);
        
        if(empty($deliveryInfo))
        {
            $error_msg    = '没有此发货单或没仓库权限,请检查';
        }
        elseif($deliveryInfo['confirm'] != 1)
        {
            $error_msg = "该发货单还未确认,不能进行操作";
            
            if($deliveryInfo['confirm'] == 2){
                $error_msg = "该发货单已被拒绝,不能进行操作";
            }
        }
        elseif($deliveryInfo['status'] !== 0)
        {
            $error_msg    = '该发货单状态不正确,不能进行操作';
            
            if($deliveryInfo['status'] == 3){
                $error_msg    = '该发货单已发货,不能进行操作';
            }
        }
        
        //错误提示
        if($error_msg)
        {
            echo json_encode(array('res'=>'error', 'msg'=>$error_msg));
            exit;
        }
        
        $deliveryInfo['order_number']  = 1;
        
        //执行发货
        $dlyProcessLib  = kernel::single('wap_delivery_process');
        $res            = $dlyProcessLib->consign($deliveryInfo);
        if($res){
            
            //task任务更新统计数据
            $wapDeliveryLib    = kernel::single('wap_delivery');
            $wapDeliveryLib->taskmgr_statistic('consign');
            
            echo json_encode(array('res'=>'succ', 'msg'=>'发货成功'));
            exit;
        }else {
            echo json_encode(array('res'=>'error', 'msg'=>'发货失败'));
            exit;
        }
    }
    
    /**
     * 重发提货校验码
     * 
     * @return json
     */
    function sendMsg()
    {
        //开启销单校验码才能重新生成校验码
        if(app::get('o2o')->getConf('o2o.delivery.confirm.code') != "true"){
            echo json_encode(array('res'=>'error', 'msg'=>'请开启销单校验码'));
            exit;
        }
        
        $delivery_bn    = $_POST['delivery_bn'];

        if(empty($delivery_bn))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'无效操作'));
            exit;
        }

        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump(array('delivery_bn'=>$delivery_bn), '*');
        if(empty($deliveryInfo))
        {
            echo json_encode(array('res'=>'error', 'msg'=>'没有相关发货单'));
            exit;
        }
        elseif($deliveryInfo['status'] != 3 || $deliveryInfo['confirm'] != 1 || $deliveryInfo['process_status'] != 7)
        {
            echo json_encode(array('res'=>'error', 'msg'=>'发货单还没有发货'));
            exit;
        }
        elseif($deliveryInfo['is_received'] == 2)
        {
            echo json_encode(array('res'=>'error', 'msg'=>'发货单已签收完成'));
            exit;
        }

        $dlyProcessLib = kernel::single('wap_delivery_process');
        $res    = $dlyProcessLib->reSendMsg($deliveryInfo);
        if($res)
        {
            echo json_encode(array('res'=>'succ', 'msg'=>'提货校验码发送成功'));
            exit;
        }
        else 
        {
            echo json_encode(array('res'=>'error', 'msg'=>'提货校验码发送失败'));
            exit;
        }
    }
    
    /**
     * 显示订单详情
     */
    function showOrderInfo()
    {
        $filehtml    = 'order/sign_order_info.html';
        
        $delivery_bn    = $_POST['delivery_bn'];
        $flag           = $_POST['flag'];
        $error_msg      = '';
        
        //先检查发货单号 和 管理员对应仓库
        if(empty($delivery_bn))
        {
            $error_msg    = '请填写发货单号';
            
            $this->pagedata['error_msg']    = $error_msg;
            $this->display($filehtml);
            exit;
        }
        
        $filter    = array('delivery_bn'=>$delivery_bn);
        
        #管理员对应仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if(!$is_super)
        {
            $branchObj     = kernel::single('o2o_store_branch');
            $branch_ids    = $branchObj->getO2OBranchByUser(true);
            if(empty($branch_ids)){
                $error_msg    = '操作员没有管辖的仓库';
                
                $this->pagedata['error_msg']    = $error_msg;
                $this->display($filehtml);
                exit;
            }
            
            $filter['branch_id'] = $branch_ids[0];
        }
        
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump($filter, '*');
        
        if(empty($deliveryInfo))
        {
            $error_msg    = '没有此发货单,请检查';
            
            $this->pagedata['error_msg']    = $error_msg;
            $this->display($filehtml);
            exit;
        }
        
        #获取发货仓库对应的门店店铺信息
        $wapDeliveryLib    = kernel::single('wap_delivery');
        $dlyProcessLib     = kernel::single('wap_delivery_process');
        $branchShopInfo    = $wapDeliveryLib->getBranchShopInfo($deliveryInfo['branch_id']);
        
        $deliveryInfo['order_number']  = 1;
        
        #合并数据
        $result    = array_merge($deliveryInfo, $branchShopInfo);
        unset($data['wms_id'], $data['store_id'], $data['area'], $data['confirm'], $data['branch_id']);
        
        //显示发货单信息
        $result['dly_status']    = $wapDeliveryLib->formatDeliveryStatus('status', $result['status']);
        $result['dly_confirm']   = $wapDeliveryLib->formatDeliveryStatus('confirm', $result['confirm']);
        
        #获取订单信息
        $orderObj    = app::get('ome')->model('orders');
        $order_info  = $orderObj->dump(array('order_bn'=>$result['order_bn']), 'order_id, pay_status, is_cod, createtime, mark_text, paytime');
        $result['order_id']    = $order_info['order_id'];
        $result['pay_name']    = ($order_info['is_cod']=='true' ? '货到付款' : '款到发货');
        
        $result['createtime']    = $order_info['createtime'];
        $result['paytime']       = $order_info['paytime'];
        
        if($order_info['mark_text']) {
            $mark_text    = unserialize($order_info['mark_text']);
            if ($mark_text) {
                foreach ($mark_text as $k=>$v) {
                    if (!strstr($v['op_time'], "-"))
                    {
                        $v['op_time']    = date('Y-m-d H:i:s',$v['op_time']);
                        $mark_text[$k]['op_time']    = $v['op_time'];
                    }
                }
            }
            $result['mark_text']    = $mark_text;
        }
        
        #获取发货单明细
        $result['delivery_items']    = $wapDeliveryLib->getDeliveryItemList($result['outer_delivery_bn']);
        
        #履约超时时间
        $result['dly_overtime']    = $wapDeliveryLib->getDeliveryOvertime($result['create_time']);
        
        //百度地图
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show == 'true'){
            $result['show_map'] = true;
        }
        
        $this->pagedata['dlyinfo']    = $result;
        $this->display($filehtml);
        exit;
    }
    
    /**
     * 签收核销列表
     */
    function sign()
    {
        $this->delivery_type    = 'sign';
        
        $sub_menu    = $this->_views_confirm($this->delivery_type);
        $filter      = $sub_menu[$this->delivery_type]['filter'];
        $title       = $sub_menu[$this->delivery_type]['label'];
        
        $offset            = $sub_menu[$this->delivery_type]['offset'];
        $limit             = $sub_menu[$this->delivery_type]['limit'];
        
        #仓库对应_发货单列表
        $wapDeliveryLib    = kernel::single('wap_delivery');
        
        $dataList          = $wapDeliveryLib->getList($filter, $offset, $limit, $sub_menu[$this->delivery_type]['orderby']);
        
        $this->pagedata['title']       = $title;
        $this->pagedata['dataList']    = $dataList;
        $this->pagedata['pageSize']    = $sub_menu[$this->delivery_type]['pageSize'];
        
        $this->pagedata['link_url']    = $sub_menu[$this->delivery_type]['href'];
        
        //baidu map button show or not
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show=="true"){
            $this->pagedata["baidu_map_show"] = true;
        }
        
        if($offset > 0)
        {
            //Ajax加载更多
            $this->display('order/order_list_more.html');
        }
        else
        {
            $this->display('order/order_list.html');
        }
    }
    
    /**
     * 核销页面
     */
    function signPage()
    {
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        
        $delivery_id    = intval($_GET['delivery_id']);
        
        //发货单
        if($delivery_id)
        {
            $deliveryInfo      = $wapDeliveryObj->dump(array('delivery_id'=>$delivery_id), '*');
        }
        
        $this->pagedata['deliveryInfo']    = $deliveryInfo;
        
        //销单校验码开关 关闭状态不显示相关校验码input/button
        if(app::get('o2o')->getConf('o2o.delivery.confirm.code') == "true"){
            $this->pagedata["code_html_show"] = true;
        }
        
        $this->display('order/sign.html');
    }
    
    /**
     * 最终签收
     * 
     * @return json
     */
    function doSign()
    {
        $delivery_bn    = $_POST['delivery_bn'];
        $flag           = $_POST['flag'];
        $error_msg      = '';
        
        //先检查发货单号 和 管理员对应仓库
        if(empty($delivery_bn))
        {
            $error_msg    = '请填写发货单号';
            
            echo json_encode(array('error'=>true, 'message'=>$error_msg, 'redirect'=>null));
            exit;
        }
        
        $filter    = array('delivery_bn'=>$delivery_bn);
        
        #管理员对应仓库
        $is_super = kernel::single('desktop_user')->is_super();
        if(!$is_super){
            $branchObj     = kernel::single('o2o_store_branch');
            $branch_ids    = $branchObj->getO2OBranchByUser(true);
            if(empty($branch_ids)){
                $error_msg    = '操作员没有管辖的仓库';
                
                echo json_encode(array('error'=>true, 'message'=>$error_msg, 'redirect'=>null));
                exit;
            }
            
            $filter['branch_id'] = $branch_ids[0];
        }
        
        $wapDeliveryObj    = app::get('wap')->model('delivery');
        $deliveryInfo      = $wapDeliveryObj->dump($filter, '*');
        
        if(empty($deliveryInfo))
        {
            $error_msg    = '没有此发货单,请检查';
        }
        elseif($deliveryInfo['status'] != 3 || $deliveryInfo['confirm'] != 1 || $deliveryInfo['process_status'] != 7)
        {
            $error_msg    = '发货单还没有发货';
        }
        elseif($deliveryInfo['is_received'] == 2)
        {
            $error_msg    = '发货单已签收完成';
        }
        
        //错误提示
        if($error_msg)
        {
            echo json_encode(array('error'=>true, 'message'=>$error_msg, 'redirect'=>null));
            exit;
        }
        
        $deliveryInfo['order_number']  = 1;
        
        //开启销单校验码需检查校验码
        if(app::get('o2o')->getConf('o2o.delivery.confirm.code') == "true"){
            $code = $_POST['sms_code'];
            if(empty($code)){
                $error_msg    = '请填写校验码';
            }
            $wapDeliveryCodeObj = app::get('wap')->model('delivery_code');
            $rs_code = $wapDeliveryCodeObj->dump(array('delivery_bn'=>$delivery_bn), '*');
            if($rs_code['code'] != $code){
                $error_msg    = '校验码填写错误';
            }elseif($rs_code['status'] == 1){
                $error_msg    = '校验码已使用';
            }
            
            //错误提示
            if($error_msg)
            {
                echo json_encode(array('error'=>true, 'message'=>$error_msg, 'flag'=>'sms_code', 'redirect'=>null));
                exit;
            }
        }
        
        //执行签收
        $dlyProcessLib    = kernel::single('wap_delivery_process');
        $res              = $dlyProcessLib->sign($deliveryInfo);
        if($res){
            
            //task任务更新统计数据
            $wapDeliveryLib    = kernel::single('wap_delivery');
            $wapDeliveryLib->taskmgr_statistic('sign');
            
            echo json_encode(array('success'=>true, 'message'=>'收货成功', 'redirect'=>$this->delivery_link['order_sign']));
            exit;
        }else {
            echo json_encode(array('error'=>true, 'message'=>'收货失败', 'flag'=>'sms_code', 'redirect'=>null));
            exit;
        }
    }
    
    /**
     * 调用百度地图接口 显示当前位置到门店的驾车路线
     */
    function showMapByBaidu(){
        $this->title = '当前位置到门店的路线';
        $this->pagedata['ship_addr'] = $_GET["store_addr"];
        $this->pagedata['baidu_map_key'] = app::get('o2o')->getConf('o2o.baidumap.ak');
        
        $this->display('order/baidumap_to_store.html');
    }
    
    /**
     * 超时订单列表
     */
    function overtimeOrders()
    {
        $this->delivery_type    = 'overtime';
        
        $sub_menu    = $this->_views_confirm($this->delivery_type);
        $filter      = $sub_menu[$this->delivery_type]['filter'];
        $title       = $sub_menu[$this->delivery_type]['label'];
        
        $offset            = $sub_menu[$this->delivery_type]['offset'];
        $limit             = $sub_menu[$this->delivery_type]['limit'];
        
        #仓库对应_发货单列表
        $wapDeliveryLib    = kernel::single('wap_delivery');
        
        $dataList          = $wapDeliveryLib->getList($filter, $offset, $limit, $sub_menu[$this->delivery_type]['orderby']);
        
        $this->pagedata['title']       = $title;
        $this->pagedata['dataList']    = $dataList;
        $this->pagedata['pageSize']    = $sub_menu[$this->delivery_type]['pageSize'];
        
        $this->pagedata['link_url']    = $sub_menu[$this->delivery_type]['href'];
        
        //baidu map button show or not
        $baidu_map_show = app::get('o2o')->getConf('o2o.baidumap.show');
        if($baidu_map_show=="true"){
            $this->pagedata["baidu_map_show"] = true;
        }
        
        if($offset > 0)
        {
            //Ajax加载更多
            $this->display('order/order_list_more.html');
        }
        else
        {
            //门店核单拒绝原因
            $reasonObj    = app::get('o2o')->model('refuse_reason');
            $refuse_reasons  = $reasonObj->getList('*', array('disabled'=>'false'), 0, 100);
            $this->pagedata['refuse_reasons']    = $refuse_reasons;
            
            $this->display('order/order_list.html');
        }
    }
}